<?php

namespace App\Http\Controllers\Admin;

use App\Models\Contract;
use App\Models\Houses;
use App\Models\Project;
use App\Models\Survey;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuditController extends Controller
{
    // 审计列表
    public function index(Request $request)
    {
        $projects = Project::all();
        $blocks = Survey::select('id', 'block_name')->get();
        $houses = Houses::all();


        $where = function ($query) use ($request) {
            if ($request->has('is_audit') and $request->is_audit != '') {
                $query->whereIsAudit($request->is_audit);
            }

            if ($request->has('project_id') and $request->project_id != '') {
                $query->whereProjectId($request->project_id);
            }

            if ($request->has('block_id') and $request->block_id != '') {
                $query->whereBlockId($request->block_id);
            }


        };


        $where_house = function ($query) use ($request) {
            if ($request->has('levy_house_num') and $request->levy_house_num != '') {
                $query->where('levy_house_num' ,$request->levy_house_num);
            }


            if ($request->has('owner') and $request->owner != '') {
                $query->where('owner', $request->owner);
            }
        };

        $audits = Contract::with(['projects' => function ($query) {
            $query->where('status', 3);

        }])->with(['houses' => function($query) use($where_house){

            $query->where($where_house);

        }])->where($where)->where('is_send_audit', 1)->paginate(10);

        return view('admin.audit.index', compact('projects', 'blocks', 'houses', 'audits'));
    }

    // 是否审计
    public function change_on(Request $request)
    {
        $id = $request->id;

        $contract = Contract::find($id);

        $value = $contract->is_audit ? false : true;
        $contract->is_audit = $value;

        $contract->save();

        if ($value == 1) {
            return ['msg' => '审计通过成功~~', 'status' => 1];
        } else {
            return ['msg' => '审计通过失败~~', 'status' => 1];
        }

    }

    // 查看合同图片
    public function show_img(Request $request)
    {
        $id = $request->id;

        $contract = Contract::select('id', 'contract_img')->find($id);
        $contract['src'] =$contract['contract_img'];

        return response()->json(['data' => [$contract] ]);

    }




}
